package com.lancoo.klgcourseware.ui.fragment.operate;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.WordArticulation;
import com.lancoo.klgcourseware.entity.bean.KlgDbConstant;
import com.lancoo.klgcourseware.entity.bean.SpellCharacterBean;
import com.lancoo.klgcourseware.ui.adapter.FillSpellCharacterAdapter;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.BasicTrainBaseFragment;
import com.lancoo.klgcourseware.utils.KlgToolUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengmi on 2019/8/28 17:11
 * Description:填空拼写引导图
 */
public class AlertSpellFillTrainOperateFragment extends AlertBaseFragment {
    protected List<SpellCharacterBean> spellCharacterBeans;
    private int hideIndex=-1,hideSecondIndex=-1,totalTrainWordCount;//隐藏第几节音节，隐藏第几节音节（备用）,总共需要训练的字母个数

    @Override
    protected int getContentViewId() {
        return R.layout.klg_fragment_alert_spell_fill_train;
    }

    @Override
    protected void initView(View view) {
        splitSpellData();
        RecyclerView recyclerView = view.findViewById(R.id.alert_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
        FillSpellCharacterAdapter mAdapter = new FillSpellCharacterAdapter(getContext(),spellCharacterBeans);
        mAdapter.setTrainModel(true,false);
        mAdapter.setAlert(true);
        recyclerView.setAdapter(mAdapter);
        view.findViewById(R.id.tv_i_know).setOnClickListener(this);

        RelativeLayout rl_spell = view.findViewById(R.id.rl_spell_content_alert);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) rl_spell.getLayoutParams();
        int topMargin=KlgToolUtils.dip2px(getContext(),KlgToolUtils.isPad(getContext())?210:202);
        layoutParams.topMargin= KlgToolUtils.getStatusHeight(getContext())+topMargin;
        rl_spell.setLayoutParams(layoutParams);
        if(getArguments().getBoolean("isOnlyShowSpellAlert")){
            view.findViewById(R.id.view_circle).setVisibility(View.INVISIBLE);
            view.findViewById(R.id.view_circle1).setVisibility(View.INVISIBLE);
            view.findViewById(R.id.rl_point).setVisibility(View.GONE);
        }
    }

    /**
     * 单词拆分，作答总时长统计
     */
    private void splitSpellData(){
        spellCharacterBeans=new ArrayList<>();
        StringBuilder stringBuilder=new StringBuilder();
        WordArticulation wordArticulation = (WordArticulation) getArguments().getSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION);
        String syllable = wordArticulation.getSyllable();
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(KlgDbConstant.SP_KNOWLEDGE_COURSE, Context.MODE_PRIVATE);
        hideIndex=sharedPreferences.getInt(KlgDbConstant.SP_FILL_SPELL_HIDE_FIRSET_INDEX,-1);
        hideSecondIndex=sharedPreferences.getInt(KlgDbConstant.SP_FILL_SPELL_HIDE_SECOND_INDEX,-1);

        int index=1;
        boolean haiPoint=false;
        boolean haiLine=false;
        String[] splitFillSpell = syllable.split("·");
        int pointShowIndex=0;
        Log.e("aaaa",syllable+ splitFillSpell.length+"隐藏第"+ hideIndex +"段,"+"隐藏第"+hideSecondIndex+"段");
        //拼写,快速环节数据组装
        for (int i = 0; i < syllable.length(); i++) {

            if(TextUtils.equals("·",syllable.charAt(i)+"")){
                haiPoint=true;
                pointShowIndex++;
                continue;
            }
            if(TextUtils.equals("-",syllable.charAt(i)+"")){
                haiLine=true;
                pointShowIndex++;
                continue;
            }
            SpellCharacterBean spellCharacterBean=new SpellCharacterBean();
            spellCharacterBean.setCharacter(syllable.charAt(i)+"");
            spellCharacterBean.setHasPoint(haiPoint);
            spellCharacterBean.setHasLine(haiLine);
            spellCharacterBean.setHideWord((hideIndex!=-1&&pointShowIndex== hideIndex) ||(hideSecondIndex!=-1&&hideSecondIndex==pointShowIndex));
            spellCharacterBeans.add(spellCharacterBean);
            if(spellCharacterBean.isHideWord()){
                stringBuilder.append(syllable.charAt(i)+" ");
            }
            index++;
            haiPoint=false;
            haiLine=false;
        }
        stringBuilder.append(" "+wordArticulation.getKlgName());
    }

    @Override
    public void onClick(View v) {
        if(System.currentTimeMillis()-time<500){
            return;
        }
        time=System.currentTimeMillis();
        super.onClick(v);
        int id=v.getId();
        if(id==R.id.tv_i_know){
            closeAlertOperate();
        }
    }
}
